package com.xbs.utils;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 获取给用户分配的权限（ajax）
 */
import com.xbs.controller.base.BaseServlet;
import com.xbs.service.content.LimitAdminService;
@WebServlet("/ajaxlimitallot")
public class AjaxLimitAllotAdminServlet extends BaseServlet {
	private LimitAdminService las;
	public AjaxLimitAllotAdminServlet() {
		las = new LimitAdminService();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		if(req.getParameter("allot")!=null&&req.getParameter("allot").length()!=0){
			//获取传过来的权限id
			String allot = req.getParameter("allot");
			/*//获取员工姓名
			String name = req.getParameter("emp");
			
			//根据姓名获取该用户的角色
			String role = las.selNameRole(name);*/
			//获取用户选择的角色
			String role = req.getParameter("role");
			//根据角色姓名获取角色id
			int id = las.selRoleId(role);
			//将获取到的权限转为数组
			String[] arrid=allot.split(",");
			//先删除该角色原有的权限
			las.deLimit(id);
			//通过遍历将新的权限写入数据库
			for(int i=0;i<arrid.length;i++){
				las.insLimit(Integer.parseInt(arrid[i]), id);
			}
		}
		
		resp.getWriter().print(true);
		
	}
}
